//头像
window.onload = function () {
    var input = document.querySelector('input[type=file]');
    input.onchange = function (e) {
        if ($('#fileicon').val() != '') {
            fd = new FormData(document.getElementById('form'));
            $.ajax({
                url: '/user/upimg',
                type: 'POST',
                timeout: 3000,
                dataType: 'JSON',
                contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                data: fd,
                contentType: false, //不设置内容类型
                processData: false, //不处理数据
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg);
                        document.getElementById('user-icon').src = data.data.url;
                        infoedit('tx', data.data.url);
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (XMLResponse) {
                    //异常捕获
                    layer.msg(XMLResponse.responseText);
                }
            });
        }
    }
}

//复制
function CopyAdd(event) {
    const range = document.createRange();
    range.selectNode(document.getElementById('copy2')); //复制对象ID
    const selection = window.getSelection();
    if (selection.rangeCount > 0) selection.removeAllRanges();
    selection.addRange(range);
    document.execCommand('copy');
    // alert("复制成功！");
    layer.msg("复制成功！")
}

//请求
function _ajax(obj, text) {
    var zh = document.getElementById('info-zh').innerHTML;
    zh = zh.replace(/^\s+|\s+$/g, "");
    var fd = new FormData();
    fd.append(obj, text);
    fd.append('name', zh);
    $.ajax({
        url: '/user/infoedit',
        type: 'POST',
        timeout: 3000,
        dataType: 'JSON',
        contentType: 'application/x-www-form-urlencoded;charset=utf-8',
        data: fd,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        success: function (data) {
            if (data.code == 200) {
                layer.msg(data.msg, {
                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                }, function () {
                    parent.location.reload();
                });
            } else {
                layer.msg(data.msg, {
                    time: 3000 //1秒关闭（如果不配置，默认是3秒）
                });
            }
        },
        error: function (XMLResponse) {
            //异常捕获
            layer.msg(XMLResponse.responseText, {
                time: 3000 //1秒关闭（如果不配置，默认是3秒）
            });
        }
    });
}

function userlv() {
    var zh = document.getElementById('info-zh').innerHTML;
    var fd = new FormData();
    fd.append('name', zh);
    $.ajax({
        url: '/user/lv',
        type: 'POST',
        timeout: 3000,
        dataType: 'JSON',
        contentType: 'application/x-www-form-urlencoded;charset=utf-8',
        data: fd,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        success: function (data) {
            if (data.code == 200) {
                layer.msg(data.msg, {
                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                }, function () {
                    // location.reload();
                    parent.location.reload();
                });
            } else {
                layer.msg(data.msg, {
                    time: 3000 //1秒关闭（如果不配置，默认是3秒）
                });
            }
        },
        error: function (XMLResponse) {
            //异常捕获
            layer.msg(XMLResponse.responseText, {
                time: 3000 //1秒关闭（如果不配置，默认是3秒）
            });
        }
    });
}

//修改信息
function infoedit(obj, val = null) {
    switch (obj) {
        case 'qm':
            var val = document.getElementById('info-qm').innerHTML;
            var tit = "个性签名随便写点啥";
            var fTy = 2;
            break;
        case 'nc':
            var val = document.getElementById('info-nc').innerHTML;
            var tit = "个人昵称";
            var fTy = 0;
            break;
        case 'yx':
            var val = document.getElementById('info-yx').innerHTML;
            var tit = "个人邮箱";
            var fTy = 0;
            break;
        case 'tx':
            _ajax(obj, val)
            var val = null;
            break;
        default:
            var val = null;
            break;
    }

    if (val != null) {
        //移除特殊空行
        val = val.replace(/^\s+|\s+$/g, "");
        layer.prompt({
            value: val,
            title: tit,
            formType: fTy
        }, function (text, index) {
            layer.close(index);
            _ajax(obj, text);
        });
    }
}

var tips;

function lvinfo(el) {
    var xs = document.getElementById('lvexp').value;
    tips = layer.tips(xs, el, {
        tips: [3, '#000'],
        time: 3000,
    });
}

function lvend() {
    layer.close(tips);
}
